package com.hltx.controller.index;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hltx.controller.base.BaseController;
import com.hltx.pojo.common.Page;
import com.hltx.service.goods.GoodsManager;
import com.hltx.service.index.IndexManager;
import com.hltx.util.ResultInfo;





/**
 * 首页controller
 * 
 * @author shibeilei
 *
 */
@Controller
public class IndexController extends BaseController{

	@Resource(name="indexService")
	private IndexManager indexService;
	
	@Resource(name="goodsService")
	private GoodsManager goodsService;
	
	
	/**
	 * 打开首页
	 */
	@RequestMapping("/index")
	public String index() {
		return "index";
	}
	
	@RequestMapping(value = "/getRecommended", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object> getRecommended(@RequestParam Map<String, String> params, HttpServletRequest request, HttpSession session){
		this.logBefore(logger, "查询推荐列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			List<Map<String, Object>> rHeadList = indexService.getRecommended(params);
			List<Map<String, Object>> rMiddleList = indexService.getRecommendedMiddle(params); 
			resMap.put("rHeadList", rHeadList);
			resMap.put("rMiddleList", rMiddleList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	
	@RequestMapping(value = "/getHotGoodsList", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object> getHotGoodsList(Page p, HttpServletRequest request, HttpSession session){
		this.logBefore(logger, "查询首页热门列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			List<Map<String, Object>> hotGoodsList = goodsService.getHotGoodsList(p);
			resMap.put("rHotGoodsList", hotGoodsList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	@RequestMapping(value = "/getClassificationList", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object> getClassificationList(@RequestParam Map<String, String> params, HttpServletRequest request, HttpSession session){
		this.logBefore(logger, "显示分类列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			List<Map<String, Object>> rClassificationList = indexService.getClassificationList(params);
			resMap.put("rClassificationList", rClassificationList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	@RequestMapping(value = "/getClassChildrenList", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object> getClassChildrenList(@RequestParam Map<String, String> params, HttpServletRequest request, HttpSession session,Page page){
		this.logBefore(logger, "显示分类列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		List<Map<String, Object>> list=new ArrayList<>();
		try{
			String parentid=params.get("parentid");
			boolean flag=isInteger(parentid);
			if(flag){
			params.put("classIds", parentid);
			list = indexService.getClassChildList(params);
			}else{
			list= indexService.getClassificationList(params);	
			}
			resMap.put("rClassificationList", list);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	@RequestMapping(value = "/hotGoodsListByParentId", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object>  HotGoodsListByParentId(Page page,String parentid){
		this.logBefore(logger, "显示分类热门");
		Map<String, Object> resMap = new HashMap<String, Object>();
		Map<String, Object> params =new HashMap<>();
		try {
			String classIds = indexService.getClassIdByPid(parentid);
			params.put("classIds", classIds);
			page.setParams(params);
			List<Map<String, Object>> list = indexService.getHotGoodsListByPid(page);
			resMap.put("rHotGoodsList", list);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	@RequestMapping(value = "/goodsListByParentId", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object>  GoodsListByParentId(Page page,String parentid,String orders){
		this.logBefore(logger, "显示分类商品");
		Map<String, Object> resMap = new HashMap<String, Object>();
		Map<String, Object> params =new HashMap<>();
		try { 
			String classIds = indexService.getClassIdByPid(parentid);
			params.put("classIds", classIds);
			params.put("order", orders);
			page.setParams(params);
			List<Map<String, Object>> list = indexService.getGoodsListByPid(page);
			resMap.put("rGoodsList", list);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	@RequestMapping(value = "/searchGoodsListByParmas", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object>  searchGoodsListByParmas(Page page){
		this.logBefore(logger, "按条件查询商品列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> rGoodsList = indexService.searchGoodsListByParmas(page);
			resMap.put("rGoodsList", rGoodsList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	@RequestMapping(value = "/searchHotGoodsListByParmas", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object>  searchHotGoodsListByParmas(Page page){
		this.logBefore(logger, "按条件查询热门商品列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> rHotGoodsList = indexService.searchHotGoodsListByParmas(page);
			resMap.put("rHotGoodsList", rHotGoodsList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	@RequestMapping(value = "/getGoodsListByParmas", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object>  getGoodsListByParmas(Page page, String parentid, String search, String orders){
		this.logBefore(logger, "按条件和分类查询商品列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		Map<String, Object> params =new HashMap<>();
		try {
			String classIds = indexService.getClassIdByPid(parentid);
			params.put("classIds", classIds);
			params.put("search", search);
			params.put("order", orders);
			page.setParams(params);
			List<Map<String, Object>> rGoodsList = indexService.getGoodsListByParmas(page);
			resMap.put("rGoodsList", rGoodsList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	@RequestMapping(value = "/getHotGoodsListByParmas", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object>  getHotGoodsListByParmas(Page page, String parentid, String search){
		this.logBefore(logger, "按条件和分类查询热门商品列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		Map<String, Object> params =new HashMap<>();
		try {
			String classIds = indexService.getClassIdByPid(parentid);
			params.put("classIds", classIds);
			params.put("search", search);
			page.setParams(params);
			List<Map<String, Object>> rHotGoodsList = indexService.getHotGoodsListByParmas(page);
			resMap.put("rHotGoodsList", rHotGoodsList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}

	  public static boolean isInteger(String str) {    
	    Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");    
	    return pattern.matcher(str).matches();    
	  } 
}
